// Copyright (C) 2010 Johan Rende.

//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

//	Official SVN repository and contact information can be found at
//	https://code.google.com/p/lt-engine/

package jwjgl;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;


public class Main {
	static boolean exit = false;
	private static Player pl1 = new Player(736,536,64,128);
//	public static Platform plat[] = new Platform[5];
	public static LevelObject[][] lvl = new LevelObject[3][3];
	public static double timerMillis = 0;
	
	static int mousex = 0;
	static int mousey = 0;
	static int scrWidth = 800;
	static int scrHeight = 600;
	static int fps = 120;
	static int currentLevelX = 0;
	static int currentLevelY = 0;
	public static int platNum;
	
	public static void initlevel(){
		Platform lv1plat[] = new Platform[4];
		lv1plat[0] = new Platform(150,128,13);
		lv1plat[1] = new Platform(150,200,4);
		lv1plat[2] = new Platform(150,400,4);
		lv1plat[3] = new Platform(Main.scrWidth/2,Main.scrHeight-64,4);
		lvl[0][0] = new LevelObject(lv1plat);
		platNum = lvl[0][0].numOfPlat;
		
	}
	public static void updatescene() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
		DrawObj.drawBackground();
//		for(int i=0;i<numOfPlat;i++){
//		plat[i].Draw();
//		}
		lvl[currentLevelX][currentLevelX].draw();
		pl1.draw();
		
	}
	 
	public static void startTimer(){
		timerMillis = System.currentTimeMillis();
	}
	public static double getTimer(){
		return(System.currentTimeMillis() - timerMillis);
	}
	
	public static void logic(){
		if(Keyboard.isKeyDown(Keyboard.KEY_UP ) && Collision.collisionDetect(pl1.plrect,2,5)!=-1){
			pl1.v = 70f;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_DOWN ) && Collision.collisionDetect(pl1.plrect,8,1)!=-1){
			
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT )){
			pl1.moveX(1);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_LEFT )){
			pl1.moveX(-1);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_ADD )){
			System.out.println("T = " + pl1.t);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT )){
			System.out.println("fps = " + fps);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_R)){
			pl1.plrect.x = scrWidth-64;
			pl1.y = scrHeight-128;
			pl1.v = 0;
		}
		if(mousex != Mouse.getX() || mousey != Mouse.getY()){
			mousex = Mouse.getX();
			System.out.println("Musens X-värde är " + Mouse.getX());
			
			mousey = (Mouse.getY());
			System.out.println("Musens Y-värde är " + (600-Mouse.getY()));
		}

		
		//Debugging junk
		if(Keyboard.isKeyDown(Keyboard.KEY_I)){
			System.out.println();
			pl1.printstats();
		}
		//print physics
		if(Keyboard.isKeyDown(Keyboard.KEY_P)){
			System.out.println("v= " + pl1.v);
			System.out.println("a=" + pl1.a);
			System.out.println("f=" + pl1.f);
			System.out.println("t=" + pl1.t);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_T)){
			System.out.println("Timer = " + getTimer());
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_L)){
			System.out.println("loc X = " + currentLevelX);
			System.out.println("loc Y = " + currentLevelY);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
			Display.destroy();
			Mouse.destroy();
			exit=true;
			System.exit(0);
		}

		if(pl1.plrect.x < 0){
			currentLevelX +=1;
			pl1.plrect.x = scrWidth;
//			levels.initlv(currentLevelX,currentLevelY);
		}
		if(pl1.plrect.x > scrWidth){
			currentLevelX -=1;
			pl1.plrect.x = 0;
//			levels.initlv(currentLevelX,currentLevelY);
		}
	}
	
	public static void main(String arg[]) throws InterruptedException {

		
		GameDisplay.initscr(scrWidth,scrHeight);
		Textures.loadtextures();
		initlevel();
		pl1.t = (float) (fps/1000.0);
		
		
		
		while(exit == false){
			startTimer();
			updatescene();
			logic();
			pl1.calculateYPos();
			pl1.checkCollision();
			System.out.println("vPix = " + pl1.vPix);
			Display.update();
			if(getTimer() < 1000/fps){
				Thread.sleep((long) ((1000/fps)-getTimer()));
			}
		}
		System.exit(0);
	}
}
/*



*/